package com.itheima.goods.controller;

import com.itheima.entity.Result;
import com.itheima.entity.StatusCode;
import com.itheima.goods.service.BrandService;
import com.itheima.pojo.Brand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author TSV
 * @description 品牌controller层
 * @date 2021/8/16
 */
@RestController
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 功能描述 查询所有
     *
     * @param
     * @return com.itheima.entity.Result
     * @auther TSV
     * @date 2021/8/16
     */
    @GetMapping("brand")
    public Result findAll() {
        return this.brandService.findAll();
    }

    /**
     * 功能描述 添加品牌
     *
     * @param brand
     * @return com.itheima.entity.Result
     * @auther TSV
     * @date 2021/8/16
     */
    @PostMapping("/brand")
    public Result add(@RequestBody Brand brand) {

        Boolean isAdd = this.brandService.add(brand);
        if (isAdd) {
            return new Result(StatusCode.OK, true, "", null);
        } else {
            return new Result(StatusCode.OK, false, "添加失败", null);
        }
    }

    /**
     * 功能描述 分页查询
     *
     * @param page
     * @param size
     * @return com.itheima.entity.Result
     * @auther TSV
     * @date 2021/8/16
     */
    @PostMapping("/brand/search/{page}/{size}")
    public Result findPage(@PathVariable("page") Long page, @PathVariable("size") Long size) {
        return this.brandService.findPage(page, size);
    }

    /**
     * 功能描述 根据id差寻品牌
     *
     * @param id
     * @return com.itheima.entity.Result
     * @auther TSV
     * @date 2021/8/16
     */
    @GetMapping("/brand/{id}")
    public Result findById(@PathVariable("id") Integer id) {

        return this.brandService.findById(id);
    }

    @PutMapping("/brand/{id}")
    public Result update(@PathVariable("id") Integer id, @RequestBody Brand brand) {
        Boolean isUpdate = this.brandService.updateBrand(id,brand);
        if (isUpdate) {
            return new Result(StatusCode.OK, true, "", null);
        } else {
            return new Result(StatusCode.OK, false, "添加失败", null);
        }
    }
    @DeleteMapping("/brand/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        Boolean isDelete = this.brandService.deleteBrand(id);
        if (isDelete) {
            return new Result(StatusCode.OK, true, "", null);
        } else {
            return new Result(StatusCode.OK, false, "添加失败", null);
        }
    }
}
